package com.jiai.cloud.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TPageModule;
import com.jiai.cloud.sys.service.ITPageModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 页面组件祥情 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2017-05-27
 */

@RestController
@RequestMapping("/api/v1/pageModule")
public class TPageModuleController {

    @Autowired
    private ITPageModuleService itPageModuleService;

    //增
    @RequestMapping(value = "/addPageModule",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object addPageModule(@RequestBody TPageModule tPageModule) throws ServiceException {
        try {
            itPageModuleService.insert(tPageModule);
            return RestResultGenerator.genSuccessResult("");
        } catch (Exception e) {
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //删
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException{
        try{
            itPageModuleService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //查询所有的
    @RequestMapping(value = "/findByAll/{page}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object findByAll(@PathVariable("page") Integer page1) throws ServiceException {
        try {
            Page<TPageModule> page = new Page<>(page1, 20);
            itPageModuleService.selectPage(page,null);
            return RestResultGenerator.genSuccessResult(new CustomPage(page));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //ID查找
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try {
            itPageModuleService.selectById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(TPageModule tPageModule)throws ServiceException{
        try {
            itPageModuleService.insertOrUpdate(tPageModule);

            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }


}
